<template>
  <div class="app-container">
    <p>
      <el-collapse accordion  v-model="activeName" >
        <el-collapse-item :name="1">
          <template slot="title">
            筛选查询
          </template>
          <div>
            <el-form :model="queryParams" ref="queryForm" label-width="100px" :inline="true" size="mini" class="search-form">
              <el-form-item label="注册时间">
                <el-date-picker
                  v-model="queryParams.registTimes"
                  type="daterange"
                  :picker-options="pickerOptions"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  align="right" />
              </el-form-item>
              <el-form-item label="最后上线">
                <el-date-picker
                  v-model="queryParams.lastTimes"
                  type="daterange"
                  :picker-options="pickerOptions"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  align="right" />
              </el-form-item>
              <el-form-item label="最近充值">
                <el-date-picker
                  v-model="queryParams.rechargeTimes"
                  type="daterange"
                  :picker-options="pickerOptions"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  align="right" />
              </el-form-item>
              <el-form-item label="充值次数">
                <el-input-number v-model="queryParams.rechargeCount" controls-position="right" :min="1" />
              </el-form-item>
              <el-form-item label="充值金额">
                <numberRange :numbers="queryParams.amountRange"/>
              </el-form-item>
              <el-form-item label="在线时长/时">
                <numberRange :numbers="queryParams.amountRange"/>
              </el-form-item>
              <el-form-item label="品类大神">
                <el-select v-model="queryParams.teamCategoryItem" placeholder="请选择">
                  <el-option v-for="item in teamCategoryOptions" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
              <el-form-item label="会员等级">
                <el-select v-model="queryParams.vipLevel" placeholder="请选择">
                  <el-option v-for="item in vipLevelOptions" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
              <el-form-item label="用户标签">
                <el-select v-model="queryParams.userCategory" placeholder="请选择">
                  <el-option v-for="item in userCategoryOptions" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>

            </el-form>
          </div>
        </el-collapse-item>
      </el-collapse>
    </p>
    

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleItem('sms')"
        >群发短信</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleItem('station')"
        >群发站内信</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleItem('push')"
        >APP推送</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleItem('category')"
        >设置标签</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleItem('coupon')"
        >赠送优惠券</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          size="mini"
          @click="handleExport"
        >导出数据</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="catUserAccountList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户昵称" align="center" prop="nick" />
      <el-table-column label="会员等级" align="center" prop="userLevel" />
      <el-table-column label="猫币余额" align="center" prop="balance" />
      <el-table-column label="猫粮余额" align="center" prop="balance" />
      <el-table-column label="订单数量" align="center" prop="channelId" />
      <el-table-column label="最近充值时间时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleInfo(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <info v-if="infoVisible" ref="info" />

    <sms v-if="smsVisible" ref="sms" />
    <station v-if="stationVisible" ref="station" />
    <push v-if="pushVisible" ref="push" />
    <coupon v-if="couponVisible" ref="coupon" />
    <category v-if="categoryVisible" ref="category" />

    
  </div>
</template>

<script>
import info from "./info"
import numberRange from "@/components/userFilter/numberRange"
import sms from "@/components/userFilter/sms"
import station from "@/components/userFilter/station"
import push from "@/components/userFilter/push"
import coupon from "@/components/userFilter/coupon"
import category from "@/components/userFilter/category"

import { listCatUserAccount, getCatUserAccount, delCatUserAccount, addCatUserAccount, updateCatUserAccount, exportCatUserAccount } from "@/api/system/catUserAccount";
import { loadTeamCategoryItem } from "@/api/system/teamCategoryItem";
import { loadUserLevel } from "@/api/system/userLevel";
import { loadUserGategory } from "@/api/system/userGategory";


export default {
  name: "CatUserAccount",
  components:{
    info,
    numberRange,
    sms,
    station,
    push,
    coupon,
    category,
  },
  data() {
    return {
      teamCategoryOptions:[],
      vipLevelOptions:[],
      userCategoryOptions:[],

      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },

      activeName:1,

      infoVisible:false,
      smsVisible:false,
      stationVisible:false,
      pushVisible:false,
      couponVisible:false,
      categoryVisible:false,

      tags: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户注册的账号表格数据
      catUserAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        registTimes: [],
        lastTimes: [],
        rechargeTimes: [],
        rechargeCount:undefined,
        amountRange:[],
        teamCategoryItem:undefined,
        vipLevel:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isOnline: [
          { required: true, message: "是否在线不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

    loadTeamCategoryItem().then(response => {
      this.teamCategoryOptions = response.data;
    });
    loadUserLevel().then(response => {
      this.vipLevelOptions = response.data;
    });
    loadUserGategory().then(response => {
      this.userCategoryOptions = response.data;
    });
  },
  methods: {
    handleItem(name){
      switch (name) {
        case 'sms':
          this.smsVisible = true
          this.$nextTick(() => {
            this.$refs.sms.init(name)
          })
          break;
        case 'station':
          this.stationVisible = true
          this.$nextTick(() => {
            this.$refs.station.init(name)
          })
          break;
        case 'push':
          this.pushVisible = true
          this.$nextTick(() => {
            this.$refs.push.init(name)
          })
          break;
        case 'coupon':
          this.couponVisible = true
          this.$nextTick(() => {
            this.$refs.coupon.init(name)
          })
          break;
        case 'category':
          this.categoryVisible = true
          this.$nextTick(() => {
            this.$refs.category.init(name)
          })
          break;
        default:
          break;
      }
    },
    changeRegistTime(e){
      console.log(e)
      // this.tags.push({ name: '标签一', type: '' })
    },
    /** 查询用户注册的账号列表 */
    getList() {
      this.loading = true;
      listCatUserAccount(this.queryParams).then(response => {
        this.catUserAccountList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户注册的账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      // 1290680031068151810
      console.log("handleUpdate row= %o", row)
      console.log("handleUpdate id = %o", id)
      getCatUserAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户注册的账号";
      });
    },
    /* 详情 */
    handleInfo(row){
      this.infoVisible = true
      this.$nextTick(() => {
        this.$refs.info.init(row)
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.form.newPassword1 && this.form.newPassword2){
        if(this.form.newPassword1 === this.form.newPassword2){
          this.form.newPassword = this.form.newPassword2
        }else{
          this.msgError("新密码设置不一致");
        }
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCatUserAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCatUserAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户注册的账号编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCatUserAccount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户注册的账号数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCatUserAccount(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style>
.search-form .el-form-item--mini.el-form-item {
    width: 450px;
}
.search-form .el-form-item__content{
    width: 350px;
}
</style>
